package com.ruoyi.roomNum.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.roomNum.mapper.RoomNumMapper;
import com.ruoyi.roomNum.domain.RoomNum;
import com.ruoyi.roomNum.service.IRoomNumService;

import javax.validation.Validator;

/**
 * 房间信息管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-20
 */
@Service
public class RoomNumServiceImpl implements IRoomNumService
{
    private static final Logger log = LoggerFactory.getLogger(RoomNumServiceImpl.class);

    @Autowired
    private RoomNumMapper roomNumMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询房间信息管理
     * 
     * @param roomId 房间信息管理主键
     * @return 房间信息管理
     */
    @Override
    public RoomNum selectRoomNumByRoomId(Long roomId)
    {
        return roomNumMapper.selectRoomNumByRoomId(roomId);
    }

    /**
     * 查询房间信息管理列表
     * 
     * @param roomNum 房间信息管理
     * @return 房间信息管理
     */
    @Override
    public List<RoomNum> selectRoomNumList(RoomNum roomNum)
    {
        return roomNumMapper.selectRoomNumList(roomNum);
    }

    /**
     * 新增房间信息管理
     * 
     * @param roomNum 房间信息管理
     * @return 结果
     */
    @Override
    public int insertRoomNum(RoomNum roomNum)
    {
        roomNum.setCreateTime(DateUtils.getNowDate());
        return roomNumMapper.insertRoomNum(roomNum);
    }

    /**
     * 修改房间信息管理
     * 
     * @param roomNum 房间信息管理
     * @return 结果
     */
    @Override
    public int updateRoomNum(RoomNum roomNum)
    {
        roomNum.setUpdateTime(DateUtils.getNowDate());
        return roomNumMapper.updateRoomNum(roomNum);
    }

    /**
     * 批量删除房间信息管理
     * 
     * @param roomIds 需要删除的房间信息管理主键
     * @return 结果
     */
    @Override
    public int deleteRoomNumByRoomIds(Long[] roomIds)
    {
        return roomNumMapper.deleteRoomNumByRoomIds(roomIds);
    }

    /**
     * 删除房间信息管理信息
     * 
     * @param roomId 房间信息管理主键
     * @return 结果
     */
    @Override
    public int deleteRoomNumByRoomId(Long roomId)
    {
        return roomNumMapper.deleteRoomNumByRoomId(roomId);
    }

    /**
     * 批量插入客房信息
     *
     * @param roomNumList 客房信息列表
     * @return
     */
    @Override
    public int batchInsertRoomNum(List<RoomNum> roomNumList) {

        return 0;
    }

    /**
     * 导入客房信息数据
     *
     * @param roomNumList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importRoomNum(List<RoomNum> roomNumList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(roomNumList) || roomNumList.size() == 0)
        {
            throw new ServiceException("导入客房信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (RoomNum roomNum : roomNumList)
        {
            try
            {
                // 验证是否存在这个用户
//                SysUser u = userMapper.selectUserByUserName(user.getUserName());
                RoomNum existingRoom = roomNumMapper.selectRoomNumByRoomNumber(roomNum.getRoomNumber());
                if (StringUtils.isNull(existingRoom))
                {
                    BeanValidators.validateWithException(validator, roomNum);

                    roomNum.setCreateBy(operName);
                    roomNum.setCreateTime(DateUtils.getNowDate());
                    roomNumMapper.insertRoomNum(roomNum);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、房间号 " + roomNum.getRoomNumber() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, roomNum);

                    roomNum.setRoomId(existingRoom.getRoomId());
                    roomNum.setUpdateBy(operName);
                    roomNum.setUpdateTime(DateUtils.getNowDate());
                    roomNumMapper.updateRoomNum(roomNum);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、房间号 " + roomNum.getRoomNumber() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、房间号 " + roomNum.getRoomNumber() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、房间号 " + roomNum.getRoomNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
